<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>SuffixArray.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="SuffixArray code in Java">
<meta NAME="TITLE" CONTENT="SuffixArray code in Java">
<meta NAME="KEYWORDS" CONTENT="SuffixArray,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>SuffixArray.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "SuffixArray.java">SuffixArray.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac SuffixArray.java</span>
<span class="comment"> *  Execution:    java SuffixArray &lt; input.txt</span>
<span class="comment"> *  Dependencies: StdIn.java StdOut.java</span>
<span class="comment"> *</span>
<span class="comment"> *  A data type that computes the suffix array of a string.</span>
<span class="comment"> *</span>
<span class="comment"> *   % java SuffixArray &lt; abra.txt</span>
<span class="comment"> *    i ind lcp rnk  select</span>
<span class="comment"> *   ---------------------------</span>
<span class="comment"> *    0  11   -   0  "!"</span>
<span class="comment"> *    1  10   0   1  "A!"</span>
<span class="comment"> *    2   7   1   2  "ABRA!"</span>
<span class="comment"> *    3   0   4   3  "ABRACADABRA!"</span>
<span class="comment"> *    4   3   1   4  "ACADABRA!"</span>
<span class="comment"> *    5   5   1   5  "ADABRA!"</span>
<span class="comment"> *    6   8   0   6  "BRA!"</span>
<span class="comment"> *    7   1   3   7  "BRACADABRA!"</span>
<span class="comment"> *    8   4   0   8  "CADABRA!"</span>
<span class="comment"> *    9   6   0   9  "DABRA!"</span>
<span class="comment"> *   10   9   0  10  "RA!"</span>
<span class="comment"> *   11   2   2  11  "RACADABRA!"</span>
<span class="comment"> *</span>
<span class="comment"> *  See SuffixArrayX.java for an optimized version that uses 3-way</span>
<span class="comment"> *  radix quicksort and does not use the nested class Suffix.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Arrays</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">SuffixArray</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a suffix array of a string of</span>
<span class="comment"> *  length </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  It supports the </span><span class="keyword">&lt;em&gt;</span><span class="comment">selecting</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th smallest suffix,</span>
<span class="comment"> *  getting the </span><span class="keyword">&lt;em&gt;</span><span class="comment">index</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th smallest suffix,</span>
<span class="comment"> *  computing the length of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">longest common prefix</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> between the</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th smallest suffix and the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-1st smallest suffix,</span>
<span class="comment"> *  and determining the </span><span class="keyword">&lt;em&gt;</span><span class="comment">rank</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> of a query string (which is the number</span>
<span class="comment"> *  of suffixes strictly less than the query string).</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses a nested class </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Suffix</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to represent</span>
<span class="comment"> *  a suffix of a string (using constant time and space) and</span>
<span class="comment"> *  </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Arrays.sort()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to sort the array of suffixes.</span>
<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">index</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> and </span><span class="keyword">&lt;em&gt;</span><span class="comment">length</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operations takes constant time </span>
<span class="comment"> *  in the worst case. The </span><span class="keyword">&lt;em&gt;</span><span class="comment">lcp</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation takes time proportional to the</span>
<span class="comment"> *  length of the longest common prefix.</span>
<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">select</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operation takes time proportional</span>
<span class="comment"> *  to the length of the suffix and should be used primarily for debugging.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For alternate implementations of the same API, see</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> SuffixArrayX}, which is faster in practice (uses 3-way radix quicksort)</span>
<span class="comment"> *  and uses less memory (does not create </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Suffix</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> objects)</span>
<span class="comment"> *  and </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="normal"> </span><span class="symbol">=</span><span class="string"> "http://algs4.cs.princeton.edu/63suffix/SuffixArrayJava6.java.html"</span><span class="keyword">&gt;</span><span class="comment">SuffixArrayJava6.java</span><span class="keyword">&lt;/a&gt;</span><span class="comment">,</span>
<span class="comment"> *  which relies on the constant-time substring extraction method that existed</span>
<span class="comment"> *  in Java 6.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/63suffix"</span><span class="keyword">&gt;</span><span class="comment">Section 6.3</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">SuffixArray</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> Suffix</span><span class="symbol">[]</span><span class="normal"> suffixes</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes a suffix array for the given </span><span class="keyword">&lt;tt&gt;</span><span class="comment">text</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> string.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> text the input string</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">SuffixArray</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> text</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> text</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">suffixes </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Suffix</span><span class="symbol">[</span><span class="normal">N</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            suffixes</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Suffix</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">);</span>
<span class="normal">        Arrays</span><span class="symbol">.</span><span class="function">sort</span><span class="symbol">(</span><span class="normal">suffixes</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Suffix</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparable</span><span class="symbol">&lt;</span><span class="normal">Suffix</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> text</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> index</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="function">Suffix</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> text</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> index</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">text </span><span class="symbol">=</span><span class="normal"> text</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">index </span><span class="symbol">=</span><span class="normal"> index</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> text</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> index</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">char</span><span class="normal"> </span><span class="function">charAt</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> text</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">index </span><span class="symbol">+</span><span class="normal"> i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compareTo</span><span class="symbol">(</span><span class="usertype">Suffix</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// optimization</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">(),</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">());</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">toString</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> text</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">index</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the length of the input string.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the length of the input string</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> suffixes</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the index into the original string of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th smallest suffix.</span>
<span class="comment">     * That is, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">text.substring(sa.index(i))</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th smallest suffix.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> i an integer between 0 and </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-1</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the index into the original string of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th smallest suffix</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IndexOutOfBoundsException unless 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> </span><span class="preproc">&amp;lt;</span><span class="comment"> </span><span class="keyword">&lt;Em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">index</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> i </span><span class="symbol">&gt;=</span><span class="normal"> suffixes</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> suffixes</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">].</span><span class="normal">index</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the length of the longest common prefix of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th</span>
<span class="comment">     * smallest suffix and the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-1st smallest suffix.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> i an integer between 1 and </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-1</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the length of the longest common prefix of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th</span>
<span class="comment">     * smallest suffix and the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-1st smallest suffix.</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IndexOutOfBoundsException unless 1 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> </span><span class="preproc">&amp;lt;</span><span class="comment"> </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">lcp</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> i </span><span class="symbol">&gt;=</span><span class="normal"> suffixes</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">lcp</span><span class="symbol">(</span><span class="normal">suffixes</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">],</span><span class="normal"> suffixes</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// longest common prefix of s and t</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">lcp</span><span class="symbol">(</span><span class="usertype">Suffix</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Suffix</span><span class="normal"> t</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">(),</span><span class="normal"> t</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> t</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> N</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment">th smallest suffix as a string.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> i the index</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> smallest suffix as a string</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IndexOutOfBoundsException unless 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;em&gt;</span><span class="comment">i</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> </span><span class="preproc">&amp;lt;</span><span class="comment"> </span><span class="keyword">&lt;Em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">select</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> i </span><span class="symbol">&gt;=</span><span class="normal"> suffixes</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> suffixes</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">].</span><span class="function">toString</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of suffixes strictly less than the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">query</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> string.</span>
<span class="comment">     * We note that </span><span class="keyword">&lt;tt&gt;</span><span class="comment">rank(select(i))</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> equals </span><span class="keyword">&lt;tt&gt;</span><span class="comment">i</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> for each </span><span class="keyword">&lt;tt&gt;</span><span class="comment">i</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * between 0 and </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-1.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> query the query string</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of suffixes strictly less than </span><span class="keyword">&lt;tt&gt;</span><span class="comment">query</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> query</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> lo </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> hi </span><span class="symbol">=</span><span class="normal"> suffixes</span><span class="symbol">.</span><span class="normal">length </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">lo </span><span class="symbol">&lt;=</span><span class="normal"> hi</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> mid </span><span class="symbol">=</span><span class="normal"> lo </span><span class="symbol">+</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">hi </span><span class="symbol">-</span><span class="normal"> lo</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">;</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> cmp </span><span class="symbol">=</span><span class="normal"> </span><span class="function">compare</span><span class="symbol">(</span><span class="normal">query</span><span class="symbol">,</span><span class="normal"> suffixes</span><span class="symbol">[</span><span class="normal">mid</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> hi </span><span class="symbol">=</span><span class="normal"> mid </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> lo </span><span class="symbol">=</span><span class="normal"> mid </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> mid</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> lo</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// compare query string to suffix</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compare</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> query</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Suffix</span><span class="normal"> suffix</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="normal">query</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">(),</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">query</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">query</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> query</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">SuffixArray</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readAll</span><span class="symbol">().</span><span class="function">replaceAll</span><span class="symbol">(</span><span class="string">"</span><span class="specialchar">\\</span><span class="string">s+"</span><span class="symbol">,</span><span class="normal"> </span><span class="string">" "</span><span class="symbol">).</span><span class="function">trim</span><span class="symbol">();</span>
<span class="normal">        </span><span class="usertype">SuffixArray</span><span class="normal"> suffix </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SuffixArray</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// StdOut.println("rank(" + args[0] + ") = " + suffix.rank(args[0]));</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"  i ind lcp rnk select"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"---------------------------"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> s</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> index </span><span class="symbol">=</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">index</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">String</span><span class="normal"> ith </span><span class="symbol">=</span><span class="normal"> </span><span class="string">"</span><span class="specialchar">\"</span><span class="string">"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> s</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">index</span><span class="symbol">,</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">min</span><span class="symbol">(</span><span class="normal">index </span><span class="symbol">+</span><span class="normal"> </span><span class="number">50</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">()))</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"</span><span class="specialchar">\"</span><span class="string">"</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">assert</span><span class="normal"> s</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">index</span><span class="symbol">).</span><span class="function">equals</span><span class="symbol">(</span><span class="normal">suffix</span><span class="symbol">.</span><span class="function">select</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">));</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> rank </span><span class="symbol">=</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">rank</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">index</span><span class="symbol">));</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%3d %3d %3s %3d %s</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">,</span><span class="normal"> index</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"-"</span><span class="symbol">,</span><span class="normal"> rank</span><span class="symbol">,</span><span class="normal"> ith</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> lcp </span><span class="symbol">=</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">lcp</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%3d %3d %3d %3d %s</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">,</span><span class="normal"> index</span><span class="symbol">,</span><span class="normal"> lcp</span><span class="symbol">,</span><span class="normal"> rank</span><span class="symbol">,</span><span class="normal"> ith</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
